<?php

	$id = $_GET['id'];
	
	if(isset($_GET['pagerel'])){$_SESSION['load']['page'] = $_GET['pagerel'];}

	$id=url_decode($id,'post');
	//GET DEFAULT QUERY 
	if(!isset($_SESSION['load']) || isset($_GET['ref'])){
		
		if(isset($_GET['ref'])){
			post_ref($id,$_GET['ref']);
		}else{
			post_ref($id);
		}
		/*
		$db=new db;
		$db->connect();
		$db->query('select count(*) as num from index_post where id >'.$id);
		$rdt=$db->fetchOne();
		$number=$rdt['num'];
		
		
		$_SESSION['load']['page']=ceil($number/LOAD_ITEMS);
		//GET TPAGE
		
		$_SESSION['load']['tpage']=floor(get_row()/LOAD_ITEMS);
		
		$_SESSION['load']['query']="select id,cat_id,post_id,username,id_album ,is_image,is_video,is_text from index_post order by  id desc";
		*/
	}

	//------
	
	
	
	$time = time();
	$date = date('Y-m-d',  $time);
	$data['post'] = get_post($id);

	$data['user'] = cache_get_user($data['post']['username']);

	$data['category']=cache_get_category($data['post']['cat_id']);
	$data['album']=cache_get_album($data['post']['album_id']);
   	$data1['title'] = $data['post']['name'];	//----- Lay tieu de dua vao head ---------

   	if(!$data['post']){
   		//echo 'khong co data';
   		// redirect hoac lam gi do
   		exit();
   	}
   	
   
   	
	// CALCULATOR VIEW --------
	
	$model = new Model();
	switch($data['post']['type']){
		case 'image':
			$is_image = true;
			$whereIndexPost = " AND is_image = 1";
			$table = 'post_image';
			break;
		case 'video':
			$is_video = true;
			$whereIndexPost = " AND is_video = 1";
			$table = 'post_media';
			break;
		case 'text':
			$is_text = true;
			$whereIndexPost = " AND is_text = 1";
			$table = 'post_text';
			break;
	}
	
	
	
	$model->where = "post_id = '" . $data['post']['id'] . "'" . $whereIndexPost;
	$model->getdata('index_post');
	$resultIndexPost = $model->fetchOne();
	/*
	 *  Get data from post table
	*/
	
	$model->where = "id = '" . $resultIndexPost['post_id'] . "'";
	$model->getdata($table);
	$result = $model->fetchOne();

	$time_views = array('views'=>(int) ($result['views'] + 1));
	// update views to record in specific table post_image/post_media/post_text
   	$model->update($table, $time_views);
   	
   	// update views to record in index_post table
   	
   	
   	/*
   	 *  Check to see if it's neccessary to update view fields to 0
   	 */
   	if(date('z', strtotime($resultIndexPost['last_update_view']) != date('z', $time))){
   		$time_views['view_day'] = 1;
   	}else{
   		
   		if(date('Y', strtotime($resultIndexPost['last_update_view'])) == date('Y', $time)){
   			$time_views['view_day'] = $resultIndexPost['view_day'] + 1;
   		}else{
   			$time_views['view_day'] = 1;
   		}
   	}

   	
   if( date('W', strtotime($resultIndexPost['last_update_view'])) != date('W', $time)){
   			$time_views['view_week'] = 1;
   }else{
	   	if(date('Y', strtotime($resultIndexPost['last_update_view'])) == date('Y', $time)){
	   		$time_views['view_week'] = $resultIndexPost['view_week'] + 1;
	   	}else{
	   		$time_views['view_week'] = 1;
	   	}
   }
   
   
   if(date('m', strtotime($resultIndexPost['last_update_view'])) != date('m', $time)){
   		$time_views['view_month'] = 1;
   }else{
   		if(date('Y', strtotime($resultIndexPost['last_update_view'])) == date('Y', $time)){
   			$time_views['view_month'] = $resultIndexPost['view_month'] + 1;
   		}else{
	   		$time_views['view_month'] = 1;
	   	}
   }
   
    $model->where = "id = '" . $id . "'";
	$time_views['last_update_view'] = $date;
   	$model->update('index_post', $time_views);
   	
   	
	/*
	 *  Select most viewed news in current day
	 */	
   	
   	$model->where = "last_update_view = '" . $date . "'";
   	$model->limit = '5';
   	$model->order = 'view_day DESC';
   	$model->getdata('index_post');
   	
   	$data['result_day_most_views'] = $model->fetchAll();
	
	//HEADER TITLE,KEYWORD,DESCRIPION
	$data1['title']=$data['post']['title'];
	$data1['keyword']=$data['post']['title'];

loadview('template/header',$data1);
if($_SESSION['mobile']==0){
loadview("news/view",$data);
}else{
	loadview("news/view_mobi",$data);
}


 //    loadview("template/right",$right);

?>